<?php
class Application_Model_Admin extends Zend_Db_Table_Abstract
{
	protected $_name = "admin";

	/**
	 * Creates the admin
	 *
	 * @param  $data containing username and password in plain text
	 * @return bool|int
	 */
	public function createAdmin($data)
	{
		if (empty($data['username']) || empty($data['pwd']) || $this -> adminExists($data['username']))
		{
			return FALSE;
		}
		else
		{
			$data['pwd'] = md5($data['pwd']);
			return $this -> insert($data);
		}
	}

	/**
	 * Checks Auth based on the admin username and password
	 * @param  $username
	 * @param  $pwd
	 * @return void
	 */
	public function checkLogin($username, $pwd)
	{
		$data = $this -> fetchRow("username='{$username}'");
		if ($data)
		{
			if (md5($pwd) == $data['pwd'])
			{
				return TRUE;
			}
			else
			{
				return FALSE;
			}
		}
		else
		{
			return FALSE;
		}
	}

	/**
	 * Check if the admin username exists already
	 * @param  $username
	 * @return bool
	 */

	function adminExists($username)
	{
		$data = $this -> fetchRow("username='{$username}'");
		if (!empty($data['username']))
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

}
